
package com.initialize.pub.service;

import com.initialize.pub.generate.dao.SjNumMapper;
import com.initialize.pub.generate.pojo.SjNum;
import com.initialize.pub.util.SMSAUtil;
import com.initialize.pub.util.SmsAService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;

/**
 * @program: star-blockchain
 * @description: 短信服务层
 * @author: Mr.Jane
 * @create: 2019-09-23 11:46
 **/
@Service
public class SmsService {

    private final
    SjNumMapper sjNumMapper;
    private final
    SmsAService smsAService;

    public SmsService(SjNumMapper sjNumMapper, SmsAService smsAService) {
        this.sjNumMapper = sjNumMapper;
        this.smsAService = smsAService;
    }

    /**
     * 发送注册验证码
     *
     * @param sjNum
     * @param stringObjectMap
     * @return
     */
    public int getSms(String sjNum, Map<String, Object> stringObjectMap) {
        String u_phone = stringObjectMap.get("u_phone").toString();
        String timestamp = stringObjectMap.get("timestamp").toString();

        //比较时间
        long time = Long.parseLong(timestamp);
        long l = System.currentTimeMillis();
        long s = (l - time) / 1000;
//        60秒短信时间,好像没用
        if (s < 60) {
            //暂时取消
//            return 2;
        }
        //准入数据库
        SjNum sjNum1 = new SjNum();
        sjNum1.setSjnum(sjNum);
        sjNum1.setId(u_phone);

        //判断是否为空
        SjNum sjNum2 = sjNumMapper.selectByPrimaryKey(u_phone);
        int i;
        if (Objects.isNull(sjNum2)) {
            i = sjNumMapper.insertSelective(sjNum1);
        } else {
            sjNum2.setSjnum(sjNum);
            i = sjNumMapper.updateByPrimaryKeySelective(sjNum1);
        }

        if (i != 0) {
            //使用阿里的短信验证
            boolean b = smsAService.smsToUser(u_phone, sjNum, SMSAUtil.REGISTER_SMS_TEMPLATE);
            if (!b) {
                return 0;
            }
        }
        return i;
    }


    /**
     * 发送找回密码验证码
     *
     * @param sjNum
     * @param stringObjectMap
     * @return
     */
    public int retrievePasswordVerificationCode(String sjNum, Map<String, Object> stringObjectMap) {
        String u_phone = getString(stringObjectMap);
        int i = getI(sjNum, stringObjectMap, u_phone);

        if (i != 0) {
            //使用阿里的短信验证
            boolean b = smsAService.smsToUser(u_phone, sjNum, SMSAUtil.RETRIEVE_PASSWORD_SMS_TEMPLATE);
            if (!b) {
                return 0;

            }
        }
        return i;
    }

    private int getI(String sjNum, Map<String, Object> stringObjectMap, String u_phone) {
        String timestamp = stringObjectMap.get("timestamp").toString();

        //比较时间
        long time = Long.parseLong(timestamp);
        long l = System.currentTimeMillis();
        long s = (l - time) / 1000;
        if (s < 60) {
            //暂时取消
//            return 2;
        }
        //准入数据库
        SjNum sjNum1 = new SjNum();
        sjNum1.setSjnum(sjNum);
        sjNum1.setId(u_phone);

        //判断是否为空
        SjNum sjNum2 = sjNumMapper.selectByPrimaryKey(u_phone);
        int i;
        if (Objects.isNull(sjNum2)) {
            i = sjNumMapper.insertSelective(sjNum1);
        } else {

            sjNum2.setSjnum(sjNum);
            i = sjNumMapper.updateByPrimaryKeySelective(sjNum1);
        }
        return i;
    }

    private String getString(Map<String, Object> stringObjectMap) {
        return stringObjectMap.get("u_phone").toString();
    }

    /**
     * 交易订单通知短信
     *
     * @return
     */
    public int tradeOrderNotificationSMSByPhone(String phone) {
        //使用阿里的短信验证
        boolean b = smsAService.smsToUser(phone, "1", "SMS_174986642");
        if (b) {
            return 0;
        }
        return 1;
    }

    /**
     * 客户端--获取当前验证码
     *
     * @param stringObjectMap
     * @return
     */
    public String getSmsByPhone(Map<String, Object> stringObjectMap) {
        String u_phone = stringObjectMap.get("u_phone").toString();
        SjNum sjNum = sjNumMapper.selectByPrimaryKey(u_phone);
        return sjNum.getSjnum();
    }


}
